package com.google.mr.okhttptest;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.orhanobut.logger.Logger;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by GR on 2017/12/15.
 * E-mail:android_gr@163.com
 * function:
 */

public class MediaplayerActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.main_state)
    TextView mainState;
    @BindView(R.id.main_time)
    TextView mainTime;
    @BindView(R.id.main_seekbar)
    SeekBar mainSeekbar;
    @BindView(R.id.main_play)
    Button mainPlay;
    @BindView(R.id.main_stop)
    Button mainStop;
    @BindView(R.id.main_quit)
    Button mainQuit;
    @BindView(R.id.main_previous)
    Button mainPrevious;
    @BindView(R.id.main_next)
    Button mainNext;
    Unbinder unbinder;
    private MusicService musicService;

    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            musicService = ((MusicService.MyBinder) iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            musicService=null;
        }
    };

    private Handler handler = new android.os.Handler();

    private SimpleDateFormat time = new SimpleDateFormat("m:ss");
    public Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (musicService.mediaPlayer.isPlaying()){
                mainState.setText(getResources().getString(R.string.musicplaying));
                mainPlay.setText(getResources().getString(R.string.musicpause).toUpperCase());
            }else{
                mainState.setText(getResources().getString(R.string.musicpause));
                mainPlay.setText(getResources().getString(R.string.musicplay).toUpperCase());
            }

            mainTime.setText(time.format(musicService.mediaPlayer.getCurrentPosition())+"/"+musicService.mediaPlayer.getDuration());
            mainSeekbar.setProgress(musicService.mediaPlayer.getCurrentPosition());
            mainSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                    if (b){
                        musicService.mediaPlayer.seekTo(seekBar.getProgress());
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
            handler.postDelayed(runnable,100);
        }
    };
    @Override
    protected void initFeature() {

    }

    @Override
    protected int layoutId() {
        return R.layout.activity_mediaplayer;
    }

    @Override
    protected void initView() {

        bindServiceConnection();

    }


    private void bindServiceConnection() {
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        bindService(intent,conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void initListener() {
        mainPlay.setOnClickListener(this);
        mainStop.setOnClickListener(this);
        mainNext.setOnClickListener(this);
        mainPrevious.setOnClickListener(this);
        mainQuit.setOnClickListener(this);
    }

    @Override
    public void onStart() {
        if (musicService.mediaPlayer.isPlaying()){
            mainState.setText(getResources().getString(R.string.musicplaying));
        }else {
            mainState.setText(getResources().getString(R.string.musicpause));
        }
        super.onStart();
    }

    @Override
    public void onResume() {
        if (musicService.mediaPlayer.isPlaying()){
            mainState.setText(getResources().getString(R.string.musicplaying));
        }else {
            mainState.setText(getResources().getString(R.string.musicpause));
        }

        mainSeekbar.setProgress(musicService.mediaPlayer.getCurrentPosition());
        mainSeekbar.setMax(musicService.mediaPlayer.getDuration());
        //发送状态给主线程改变播放状态
        handler.post(runnable);
        super.onResume();
    }

    @Override
    protected void initData() {

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(conn);
        handler.removeCallbacks(runnable);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_play:
                Logger.e("play or pause");
                musicService.playOrPause();
                break;
            case R.id.main_stop:
                musicService.stop();
                break;
            case R.id.main_previous:
                musicService.previousMusic();
                break;
            case R.id.main_next:
                musicService.nextMusic();
                break;
            case R.id.main_quit:
                //移除子线程中的播放状态
                //handler
                unbindService(conn);
                System.exit(0);
                break;
        }
    }
}
